# -*- coding: utf-8 -*-

import sys
sys.path.append('../')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests
from common.helper import Helper
from urllib.parse import urlencode
class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "阳光出行保交通工具意外险"
    # 生成产品数据
    def genProductInfo(self):
        plan = {
            "planA": {
                # 飞机伤残
                "airplane_death": ["200000"],
                # 火车伤残
                "train_death": ["200000"],
                # 轮船伤残
                "ship_disability": ["200000"],
                # 客运伤残
                "passenger_death": ["200000"],
                # 保障时间
                "days": ["1m", "3m", "1y"],
                # 保险人数
                "people_count": ["1"],
            },
            "planB": {
                # 飞机伤残
                "airplane_death": ["300000"],
                # 火车伤残
                "train_death": ["300000"],
                # 轮船伤残
                "ship_disability": ["300000"],
                # 客运伤残
                "passenger_death": ["300000"],
                # 保障时间
                "days": ["1m", "3m", "1y"],
                # 保险人数
                "people_count": ["1"],
            },
            "planC": {
                # 飞机伤残
                "airplane_death": ["500000"],
                # 火车伤残
                "train_death": ["500000"],
                # 轮船伤残
                "ship_disability": ["500000"],
                # 客运伤残
                "passenger_death": ["500000"],
                # 保障时间
                "days": ["1m", "3m", "1y"],
                # 保险人数
                "people_count": ["1"],
            }

        }
        for k in plan:
            for v in product(plan[k]["airplane_death"], plan[k]["train_death"], plan[k]["ship_disability"],
                             plan[k]["passenger_death"], plan[k]["days"], plan[k]["people_count"]):
                data = Helper.formatProductInfo(plan[k], v)
                yield data

    # 测算保费
    def calcPremium(self):
        params = '''
        agentId: 
        sessionKey: myInsurance1474e624-75ed-4bc7-9d4d-1a938719b532
        pagePropertyObj.selectedPlanId: 2364
        source: 
        fuwu: 
        sso: true
        processId: buy-process-d1f72273-7f7b-4e36-85aa-de634354381c
        orderSource: product-center
        platform: pc
        statisticProductType: 01
        statisticProductId: 1618
        verifycode: 
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].clauseId: 1641
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[0].liabilityId: 1836
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[0].three: 
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[0].isSelected: true
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[0].amount: 30
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[1].liabilityId: 1837
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[1].three: 
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[1].isSelected: true
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[1].amount: 30
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[2].liabilityId: 1838
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[2].three: 
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[2].isSelected: true
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[2].amount: 30
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[3].liabilityId: 1839
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[3].three: 
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[3].isSelected: true
        pagePropertyObj.pageProduct.ecInsurancplans[0].ecClauses[0].ecLiabilities[3].amount: 30
        pagePropertyObj.insuPeriod: 3
        pagePropertyObj.insuStartDate: 2020-03-27
        pagePropertyObj.insuPeriodUnit: 02
        pagePropertyObj.insuEndDate: 2020-06-26
        '''
        data = Helper.perfectJson(params)

        for v in self.genProductInfo():
            if v["days"] == "1m":
                days = "1"
                date = "2020-04-26"
            elif v["days"] == "3m":
                days = "3"
                date = "2020-06-26"
            else:
                days = "12"
                date = "2021-03-26"

            data["pagePropertyObj.pageProduct." \
                 "ecInsurancplans[0].ecClauses[0].ecLiabilities[0].amount"] = str(int(int(v["airplane_death"]) / 10000))
            data["pagePropertyObj.pageProduct." \
                 "ecInsurancplans[0].ecClauses[0].ecLiabilities[1].amount"] = str(int(int(v["train_death"]) / 10000))

            data["pagePropertyObj.pageProduct." \
                 "ecInsurancplans[0].ecClauses[0].ecLiabilities[2].amount"] = str(
                int(int(v["ship_disability"]) / 10000))

            data["pagePropertyObj.pageProduct." \
                 "ecInsurancplans[0].ecClauses[0].ecLiabilities[3].amount"] = str(
                int(int(v["passenger_death"]) / 10000))
            data["pagePropertyObj.insuPeriod"] = days
            data["pagePropertyObj.insuEndDate"] = date
            payload = urlencode(data).replace("+", "")
            r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
            if r.status_code == 200 and "产品报价出错，请刷新页面！" not in r.text :
                price = Helper.decimalParse(r.json()["pagePropertyObj"]["totalPremium"])
                result = {
                    "name":self.name,
                    "company":"阳光保险集团",
                    "price":price
                }

                v.update( result  )
                Helper.mergeTable( self.name,self.mysql,v )
